/*
* Copyright (c) 2025 Huawei Device Co., Ltd.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including but not limited to the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to provide persons with access to the Software
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGE OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

<template>
  <div ref='transformImgRef' class='transformImg-container'>
    <img :src='imgSrc' alt='image' class='transformImg-content' :style='imgStyle' />
  </div>
</template>

<script lang='ts'>
import {
  PointerGestureManager,
  PointerOptions,
  GestureHandlers,
  PointerState,
} from '@hadss/web_uni_input';

export default {
  name: 'ImageViewer',
  props: {
    imgSrcProp: { type: String, required: true }
  },
  data() {
    return {
      imgSrc: this.imgSrcProp,
      instance: null as any,
      scale: 1,
      minScale: 0.3,
      maxScale: 1,
      rotation: 0,
      translateX: 0,
      translateY: 0,
      lastRotation: 0,
    };
  },
  computed: {
    imgStyle() {
      return {
        transform: `translate(${this.translateX}px, ${this.translateY}px) scale(${this.scale}) rotate(${this.rotation}deg)`,
        'transform-origin': 'center center'
      };
    }
  },
  methods: {
    getDistance(p1, p2) {
      const dx = p2.globalX - p1.globalX;
      const dy = p2.globalY - p1.globalY;
      return Math.hypot(dx, dy);
    },
    getAngle(p1, p2) {
      const dx = p2.globalX - p1.globalX;
      const dy = p2.globalY - p1.globalY;
      return Math.atan2(dy, dx) * 180 / Math.PI;
    },
    rotateStart(e: PointerState) {
      if (e.fingerList.length === 1) {
        return;
      } else if (e.fingerList.length === 2) {
        this.startDist = this.getDistance(e.fingerList[0], e.fingerList[1]);
        this.startAngle = this.getAngle(e.fingerList[0], e.fingerList[1]);
        this.lastScale = this.scale;
        this.lastRotation = this.rotation;
      }
    },
    rotateMove(e: PointerState) {
      if (e.fingerList.length === 1) {
        return;
      } else if (e.fingerList.length === 2) {
        const newDist = this.getDistance(e.fingerList[0], e.fingerList[1]);

        let newScale = this.lastScale * (newDist / this.startDist);
        newScale = Math.max(this.minScale, Math.min(this.maxScale, newScale));
        this.scale = newScale;

        const newAngle = this.getAngle(e.fingerList[0], e.fingerList[1]);
        const diff = newAngle - this.startAngle;
        this.rotation = this.lastRotation + diff;
      }
    },
    rotateEnd(e) {
      //旋转结束后
    },
  },
  mounted() {
    const handlers: GestureHandlers = {
      onRotateStart: (e) => {
        this.rotateStart(e);
      },
      onRotateMove: (e) => {
        this.rotateMove(e);
      },
      onRotateEnd: (e) => {
        this.rotateEnd(e);
      },
    };
    const configs: PointerOptions = {
      enableScale: true,
      enableRotate: true,
    };

    if (this.$refs.transformImgRef) {
      this.instance = PointerGestureManager(
        this.$refs.transformImgRef,
        handlers,
        configs
      );
    }
  },
  beforeDestroy() {
    if (this.instance) {
      this.instance.destroy();
    }
  },
};
</script>

<style scoped>
@import './index.less';
</style>